package com.cct.digitalarchapi.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cct.digitalarchapi.common.Response;
import com.cct.digitalarchapi.dao.ArchDeptMapper;
import com.cct.digitalarchapi.dao.StaffListMapper;
import com.cct.digitalarchapi.pojo.ArchDept;
import com.cct.digitalarchapi.pojo.ArchDeptCategory;
import com.cct.digitalarchapi.pojo.vo.ArchDeptVo;
import com.cct.digitalarchapi.service.ArchDeptService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @FileName ArchCategoryServiceImpl
 * @Description 档案分类实现类
 * @Author Created by chenmingzhe
 * @Date 2023/11/19
 **/
@Slf4j
@Service
public class ArchDeptServiceImpl implements ArchDeptService {
    @Autowired
    private ArchDeptMapper archDeptMapper;
    @Autowired
    private StaffListMapper  staffListMapper;

    /**
     * 获取所有部门
     */
    @Override
    public Response getAllArchDept(int pageNum, int pageSize) {
        try {
            Page<ArchDeptVo> page = new Page<>(pageNum, pageSize);
            archDeptMapper.getAllArchDept(page);
            return Response.success(page);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("查询所有部门分类失败：{}", e.getMessage());
            return Response.error("查询所有部门分类失败");
        }

    }

    /**
     * 新增部门分类
     */
    @Override
    public Response addArchDept(ArchDept archDept) {
        try {
            int num = archDeptMapper.queryHaveSameName(archDept.getArchDeptName());
            if (num >= 1) {
                return Response.bad("部门名字已存在，请重新输入！");
            }
            //取最后一条记录的code
            ArchDept dept = archDeptMapper.selectLastArchDept();
            if (dept != null) {
                int archDeptCode = Integer.parseInt(dept.getArchDeptCode()) + 1;
                if (archDeptCode >= 10) {
                    archDept.setArchDeptCode(String.valueOf(archDeptCode));
                } else {
                    archDept.setArchDeptCode("0" + String.valueOf(archDeptCode));
                }
            } else {
                archDept.setArchDeptCode("01");
            }
            if ("".equals(archDept.getManager())) {
                archDept.setManager(null);
            }
            archDeptMapper.addArchDept(archDept);
            return Response.success("");
        } catch (Exception e) {
            e.printStackTrace();
            log.error("新增部门分类失败：{}", e.getMessage());
            return Response.error("新增部门分类失败");
        }
    }

    /**
     * 删除一个部门分类
     *
     * @param archDeptId
     */
    @Override
    public Response deleteArchDept(int archDeptId) {
        try {
            archDeptMapper.deleteArchDept(archDeptId);
            return Response.success("");
        } catch (Exception e) {
            e.printStackTrace();
            log.error("删除部门分类失败：{}", e.getMessage());
            return Response.error("删除部门分类失败");
        }
    }


    @Override
    public Response updateArchDept(ArchDept archDept) {
        try {
            archDeptMapper.updateArchDept(archDept);
            ArchDept dept=archDeptMapper.getArchDeptById(archDept.getArchDeptId());
            if(!(archDept.getManager()).equals(dept.getManager())){
                archDeptMapper.updateManager(archDept.getArchDeptId(),archDept.getManager());
                int staffId=staffListMapper.getStaffIdByCode(archDept.getManager());
                staffListMapper.clearManage(archDept.getArchDeptName());
                staffListMapper.updateManage(staffId,archDept.getArchDeptName());
            }
            return Response.success("");
        } catch (Exception e) {
            e.printStackTrace();
            log.error("修改部门信息失败：{}", e.getMessage());
            return Response.error("修改部门信息失败");
        }
    }
    @Transactional(rollbackFor = Exception.class)
    @Override
    public Response setManager(int archDeptId, int staffId, String manager, String archDeptName) {
        try {
            archDeptMapper.updateManager(archDeptId,manager);
            staffListMapper.updateManage(staffId,archDeptName);
            return Response.success("");
        } catch (Exception e) {
            e.printStackTrace();
            log.error("设置为主管失败：{}", e.getMessage());
            return Response.error("设置为主管失败");
        }
    }

    @Override
    public Response getAllDept() {
        try {
            List<ArchDept> list=archDeptMapper.getAllDept();
            return Response.success(list);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("查询部门失败：{}", e.getMessage());
            return Response.error("查询部门失败");
        }
    }


}
